package fun.diaoer.config.security;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;

import fun.diaoer.common.Contants;
import fun.diaoer.common.JsonResult;
import fun.diaoer.entity.DISysPermission;
import fun.diaoer.service.AuthService;
import fun.diaoer.service.security.DiaoerUserDetail;
import lombok.extern.slf4j.Slf4j;

/**
 * 成功处理
 * @author Administrator
 *
 */
@Slf4j
@Component
public class DiaoerAuthenctiationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
	
	@Autowired
	private AuthService authService;
	
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,Authentication authentication) throws IOException, ServletException {
    	 
    	List<DISysPermission> perms=authService.searchPerms();
    	DiaoerUserDetail dud=(DiaoerUserDetail) authentication.getPrincipal();
    	dud.setPerms(perms);
    	Contants.buildLoginResponse(request, response);
    	log.error("登录成功");
        JsonResult<String> jr=new JsonResult<>();
		jr.setCode(200);
		jr.setMsg("登录成功");
		response.getWriter().write(JSON.toJSONString(jr));
    }

}
